package com.coocaa.ops.admin.v1.timedtask.service.impl;

import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.v1.timedtask.constant.TimedTaskTypeConstants;
import com.coocaa.ops.admin.v1.timedtask.job.impl.MyQuartzJobServiceImpl;
import com.coocaa.ops.admin.v1.timedtask.model.QuartzSchedulerModel;
import com.coocaa.ops.admin.v1.timedtask.model.TimedQueryModel;
import com.coocaa.ops.admin.v1.timedtask.provider.impl.TimedTaskProviderImpl;
import com.coocaa.ops.admin.v1.timedtask.service.ITimedTaskQueryService;
import com.coocaa.ops.common.core.base.model.PageResultModel;
import com.coocaa.ops.common.core.base.model.R;
import com.coocaa.ops.common.core.base.util.PageQueryUtil;
import com.coocaa.ops.common.core.biz.timedtask.entity.TimedTaskInfoEntity;
import com.coocaa.ops.common.tool.json.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author bijiahao
 * @date : 2019/8/7.
 * @description 定时信息查询 服务接口实现
 */
@Service
public class TimedTaskQueryServiceImpl implements ITimedTaskQueryService {
    @Autowired
    private TimedTaskProviderImpl timedTaskProvider;
    @Autowired
    private MyQuartzJobServiceImpl myQuartzJobService;
    private static CommonLanguageProperties commonLanguageProperties;

    @Override
    public PageResultModel<TimedTaskInfoEntity> pageList(TimedQueryModel model) {
        Map<String, Object> params = PageQueryUtil.getFilterMap(model);
        return timedTaskProvider.getTimedTaskBiz().getCommonPageList(params, false);
    }

    @Override
    public R getQuartzSchedulerInfo() throws Exception {
        QuartzSchedulerModel quartzSchedulerModel = myQuartzJobService.getQuartzSchedulerInfo();
        System.out.println(JsonUtil.obj2Json(quartzSchedulerModel));
        return R.ok().data(quartzSchedulerModel);
    }

    @Override
    public R getDetailInfo(Integer id) {
        TimedTaskInfoEntity entity = timedTaskProvider.getTimedTaskBiz().queryById(id);
        if (TimedTaskTypeConstants.isRecommendPoolUpdate(entity.getTimedType())) {
            return R.ok().data(entity);
        }
        return R.fail(commonLanguageProperties.getNotSupport());
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        TimedTaskQueryServiceImpl.commonLanguageProperties = commonLanguageProperties;
    }
}
